package com.Smart.smartscals.DataBase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;



public class DataBaseService {

	private SqliteDataHelper  dbhelper;
	private static DataBaseService dataService = null;
	private DataBaseService(Context context){
		dbhelper = new SqliteDataHelper(context);
	}

	public static DataBaseService getInstance(Context context){
		if(dataService==null){
			dataService = new DataBaseService(context);
		}
		return dataService;
	}
	/**
	 * 重置数据的方法
	 * @param table_name
	 */
	public void dropTable(String table_name){
		dbhelper.getWritableDatabase().execSQL(   
				"DROP TABLE IF EXISTS " + table_name); 
	}

	public void closeDataBase(String databasename){
		dbhelper.getWritableDatabase().close();
	}
	public void insertUserInfo(String userName,String pwd,String birThday,String sex,String height){
		SQLiteDatabase userInfodb = dbhelper.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("userName", userName);
		cv.put("passWord", pwd);
		cv.put("birthday", birThday);
		cv.put("userSex", sex);
		cv.put("userHeight", height);
		userInfodb.insert("UserInfo", null, cv);

	}


	public void insertBodyInfo(long date,float weight,float fat,int weekNUM,int monthNUM,int dayNUM,int yearNUM,String userName) {
		SQLiteDatabase userInfodb = dbhelper.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("date", date);
		cv.put("weight", weight);	
		cv.put("bodyFat", fat);
		cv.put("weekNUM", weekNUM);
		cv.put("monthNUM", monthNUM);
		cv.put("dayNUM", dayNUM);
		cv.put("yearNUM", yearNUM);
		cv.put("userName", userName);
		userInfodb.insert("BodyInfo", null, cv);
	}
	public void insertLoadTimeInfo(String userName,String mloadTime) {
		SQLiteDatabase userInfodb = dbhelper.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("userName", userName);
		cv.put("loadTime", mloadTime);	
		userInfodb.insert("loadTimeInfo", null, cv);
	}

	public Cursor seletBodyInfo(long startDate,long endDate,String userName){
		String str = "select * from BodyInfo where userName=? and date>=? and date<=?";
		Cursor cur = dbhelper.getReadableDatabase().rawQuery(str, new String[]{userName,Long.toString(startDate),Long.toString(endDate)});
		return cur;			
	}
	/**
	 * 取出数据库中最后的一条数据
	 * @return
	 */
	public Cursor getLastData(String muserName){
		String str = "select * from BodyInfo where userName = ? ";
		Cursor cursor = dbhelper.getReadableDatabase().rawQuery(str, new String[]{muserName});
		return cursor;
	}

	public Cursor getLastUser(){
		String str = "select * from UserInfo";
		Cursor cursor = dbhelper.getReadableDatabase().rawQuery(str, null);
		return cursor;
	}

	/**
	 * 更新数据库的方法
	 * @param date
	 * @param weight
	 * @param bodyfat
	 * @param result
	 */
	public void updateBodyInfo(long date,float weight,float bodyfat,int weekNum,int monthNum,int dayNUM,int yearNum,String userName){
		SQLiteDatabase db = dbhelper.getWritableDatabase();
		ContentValues  cv = new ContentValues();
		cv.put("weight", weight);
		cv.put("bodyFat", bodyfat);
		cv.put("weekNUM", weekNum);
		cv.put("monthNUM", monthNum);
		cv.put("dayNUM", dayNUM);
		cv.put("yearNUM", yearNum);
		cv.put("userName", userName);
		db.update("BodyInfo", cv, "date=?", new String[]{date+""}); 			
	}
	/**
	 * 获得最后一次下载文件的时间
	 * @param muserName
	 * @return
	 */
	public Cursor getLastLoadTime(String muserName){
		String str = "select * from loadTimeInfo where userName = ? ";
		Cursor cursor = dbhelper.getReadableDatabase().rawQuery(str, new String[]{muserName});
		return cursor;
	};

	public void close() {   
		dbhelper.close();   
	} 



}
